package org.biliviz.be.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.biliviz.be.entity.AuthorInfo;
import org.biliviz.be.mapper.AuthorInfoMapper;
import org.biliviz.be.service.AuthorInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sg
 * @since 2024-03-02
 */
@Service
public class AuthorInfoServiceImpl extends ServiceImpl<AuthorInfoMapper, AuthorInfo> implements AuthorInfoService {

    @Override
    public List<AuthorInfo> getTop100() {
        QueryWrapper<AuthorInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("c_fans"); // 按 CFans 字段降序排序
        queryWrapper.last("LIMIT 100"); // 添加限制条件，仅获取前100条记录
        List<AuthorInfo> top100Authors = baseMapper.selectList(queryWrapper);
        return top100Authors;
    }

    @Override
    public List<AuthorInfo> getDayFansIncreaseTop100() {
        QueryWrapper<AuthorInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("fans_day_change"); // 按 CFans 字段降序排序
        queryWrapper.last("LIMIT 100"); // 添加限制条件，仅获取前100条记录

        List<AuthorInfo> dayFansDecreaseTop100 = baseMapper.selectList(queryWrapper);

        return dayFansDecreaseTop100;
    }

    @Override
    public List<AuthorInfo> getDayFansDecreaseTop100() {
        QueryWrapper<AuthorInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("fans_day_change"); // 按 CFans 字段降序排序
        queryWrapper.last("LIMIT 100"); // 添加限制条件，仅获取前100条记录

        List<AuthorInfo> dayFansDecreaseTop100 = baseMapper.selectList(queryWrapper);

        return dayFansDecreaseTop100;
    }

   @Override
    public List<AuthorInfo> getAuthorTop100ID(int type) {
        QueryWrapper<AuthorInfo> queryWrapper = new QueryWrapper<>();
       // 根据 type 不同的值设置排序字段和添加非空判断条件
       if (type == 3) {
           queryWrapper.orderByDesc("fans_month_change").isNotNull("fans_month_change");
       } else if (type == 2) {
           queryWrapper.orderByDesc("fans_week_change").isNotNull("fans_week_change");
       } else if (type == 1) {
           queryWrapper.orderByDesc("fans_day_change").isNotNull("fans_day_change");
       } else if (type == -1) {
           queryWrapper.orderByAsc("fans_day_change").isNotNull("fans_day_change");
       } else if (type == -2) {
           queryWrapper.orderByAsc("fans_week_change").isNotNull("fans_week_change");
       } else if (type == -3) {
           queryWrapper.orderByAsc("fans_month_change").isNotNull("fans_month_change");
       } else if (type == 0) {
           queryWrapper.orderByDesc("c_fans");
       }

       queryWrapper.last("LIMIT 100"); // 添加限制条件，仅获取前100条记录


       List<AuthorInfo> dayFansDecreaseTop100 = baseMapper.selectList(queryWrapper);

        return dayFansDecreaseTop100;
    }

    @Override
    public AuthorInfo getAuthorInfo(Long mid) {
        QueryWrapper<AuthorInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mid", mid);
        AuthorInfo author = baseMapper.selectOne(queryWrapper);
        return author;

    }
    @Override
    public List<AuthorInfo> getAuthorInfoLike(String name) {
        QueryWrapper<AuthorInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("mid", "%" + name + "%");  // 使用 like 进行模糊查询
        queryWrapper.or();
        queryWrapper.like("name", "%" + name + "%");
        List<AuthorInfo> author = baseMapper.selectList(queryWrapper);
        return author;
    }
}
